package cc.rengu.igas.share.core.service.trans;

import cc.rengu.igas.channel.wechat.facade.request.FinancialTransMsgPushRequest;
import cc.rengu.igas.channel.wechat.facade.response.FinancialTransMsgPushResponse;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.TxnNoticeInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.TxnNoticeInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.TxnNoticeInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.TxnInfo;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.GlobalTransUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class TransNoticeService extends DubboAsynBaseService {
    @BIZ_PROCESS
    public void proceWechatNoticeResult() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        FinancialTransMsgPushRequest financialTransMsgPushRequest = (FinancialTransMsgPushRequest) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_REQUEST, FinancialTransMsgPushRequest.class);
        MchntOrderInfo mchntOrderInfo = (MchntOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.MCHNT_ORDER_INFO);
        String responseStr = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.DUBBO_RESPONSE);
        Result<FinancialTransMsgPushResponse> result = JSON.parseObject(responseStr, new TypeReference<Result<FinancialTransMsgPushResponse>>() {
        });
        String instId = financialTransMsgPushRequest.getHeader().getInstId();
        TxnNoticeInfoMapper txnNoticeInfoMapper = new TxnNoticeInfoMapperImpl();
        /* 判断推送状态 */
        if (null == result || !result.isSuccess() || !OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(result.getResult().getRespCode())) {

            /* 查询登记通知流水 */
            TxnNoticeInfo txnNoticeInfo = txnNoticeInfoMapper.selectTxnNoticeInfoByPrimaryKey(instId, mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntNo(), "00", financialTransMsgPushRequest.getOpenId());
            txnNoticeInfo.setTryMaxTimes(txnNoticeInfo.getTryTimes() + 1);
            txnNoticeInfo.setLastNoticeTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            txnNoticeInfo.setNoticeStaus("02");
            int iReturnCode = txnNoticeInfoMapper.updateTxnNoticeInfoByPrimaryKey(txnNoticeInfo);
            if (0 != iReturnCode) {
                rglog.warn("更新商城通知信息失败,通知失败! iReturnCode:<{}>", iReturnCode);
            }
        } else {

            /* 查询登记通知流水 */
            TxnNoticeInfo txnNoticeInfo = txnNoticeInfoMapper.selectTxnNoticeInfoByPrimaryKey(instId, mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntNo(), "00", financialTransMsgPushRequest.getOpenId());
            txnNoticeInfo.setTryMaxTimes(txnNoticeInfo.getTryTimes() + 1);
            txnNoticeInfo.setLastNoticeTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            txnNoticeInfo.setNoticeStaus("01");
            int iReturnCode = txnNoticeInfoMapper.updateTxnNoticeInfoByPrimaryKey(txnNoticeInfo);
            if (0 != iReturnCode) {
                rglog.warn("更新商城通知信息成功,通知成功! iReturnCode:<{}>", iReturnCode);
            }

        }
    }

}
